package com.genokon.provider.controller;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author by baichaoda
 * @Date 2020/7/22 14:28
 */
@RestController
public class EchoController {
    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("/getInstances")
    public List<ServiceInstance> getInstances(){
        return discoveryClient.getInstances("service-provider");
    }
    @GetMapping("/")
    public ResponseEntity<String> index(){
        return new ResponseEntity<String>("index error", HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @GetMapping("/test")
    public ResponseEntity<String> test(){
        return new ResponseEntity<String>("error", HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @GetMapping("/sleep")
    public String sleep(){
        try {
            Thread.sleep(1000L);
        }catch (InterruptedException e){
            e.printStackTrace();

        }
        return "ok";
    }
    @GetMapping("/hello")
    public String hello(){
        return "hello" ;
    }

    @GetMapping("/echo/{string}")
    public String echo(@PathVariable String string, HttpServletRequest request){
        return "hello Nacos discovery " + string + ". Port is " + request.getLocalPort() + ". ";
    }

    @GetMapping("/divide")
    public String divide(@RequestParam Integer a, @RequestParam Integer b){
        return String.valueOf(a / b);
    }
}
